package editor;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.awt.geom.AffineTransform;
import java.util.LinkedList;

import javax.swing.*;

public class TilesetDisplay extends JDialog implements ActionListener
{
	private JComboBox tilesetsMenu;
	private JComboBox layersMenu;
	private int currentLayer;
	private JButton edit;
	private LinkedList<Tileset> tilesets;
	private Tileset current;
	private View panel;
	
	private UI parent;
	
	public TilesetDisplay( UI parent )
	{
		super( parent );
		
		this.parent = parent;
		
		this.setSize( 200, 300 );
		
		tilesets = new LinkedList<Tileset>();
		
		tilesetsMenu = new JComboBox( new String[]{ "Choose a tileset..." } );		
		layersMenu = new JComboBox( new String[]{ "Layer 1", "Layer 2", "Layer 3" } );
		edit = new JButton( "Edit Tileset..." );
		edit.setEnabled( false );
		
		currentLayer = 1;
		
		panel = new View();
		panel.setPreferredSize( new Dimension( 32 * 4, 32 * 4 ) );
		
		GridBagLayout layout = new GridBagLayout();
		
		addComponent( tilesetsMenu, 1, 1, 1, 1, layout );
		addComponent( layersMenu, 	1, 2, 1, 1, layout );
		addComponent( panel, 		1, 3, 1, 1, layout );
		addComponent( edit, 		1, 4, 1, 1, layout );
		
		this.setLayout( layout );
		
		tilesetsMenu.addActionListener( this );
		layersMenu.addActionListener( this );
		edit.addActionListener( this );
		
		this.setVisible( true );
	}
	
	private void addComponent( Component c, int col, int row, int width, int height, GridBagLayout layout )
	{
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = col;
		gbc.gridy = row;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		gbc.weightx = 100;
		gbc.weighty = 100;
		layout.setConstraints( c, gbc );
		gbc.insets = new Insets( 5, 5, 5, 5 );
		this.add( c );
	}
	
	public void addTileset( Tileset ts )
	{
		tilesetsMenu.addItem( ts.getName() );
		tilesets.add( ts );
	}
	
	public int currentLayer()
	{
		return currentLayer;
	}

	public void actionPerformed( ActionEvent e ) 
	{
		Object o = e.getSource();
		
		if( o.equals( tilesetsMenu ) )
		{
			String name = (String)tilesetsMenu.getSelectedItem();
			
			for( Tileset ts : tilesets )
			{
				if( ts.getName().equals( name ) )
				{
					current = ts;
					edit.setEnabled( true );
					panel.repaint(); 
					break;
				}
				else
				{
					edit.setEnabled( false );
				}
			}
		}
		else if( o.equals( layersMenu ) )
		{
			String name = (String)layersMenu.getSelectedItem();
			
			if( name.equals( "Layer 1" ) )
			{
				currentLayer = 1;
			}
			else if( name.equals( "Layer 2" ) )
			{
				currentLayer = 2;
			}
			else if( name.equals( "Layer 3" ) )
			{
				currentLayer = 3;
			}
			parent.repaint();
		}
		else if( o.equals( edit ) )
		{
			TilesetDialog tsd = new TilesetDialog( (JFrame)this.getParent(), current );
			int i = tilesets.indexOf( current );
			tilesets.remove( current );
			tilesets.add( i, tsd.getEdited() );
			panel.repaint();
		}
	}

	
	private class View extends JPanel implements MouseListener
	{
		public View()
		{
			this.addMouseListener( this );
		}
		
		public void paintComponent( Graphics g )
		{
			if( current != null )
			{
				int x = 0, y = 0;
				for( Tile t : current.getTiles() )
				{
					t.getImage().draw( g, AffineTransform.getTranslateInstance( x * 32,  y * 32 ) );
					x++;
					if( x > 3 )
					{
						x = 0;
						y++;
					}
				}
			}
		}

		public void mouseClicked( MouseEvent e ) 
		{
			int x = e.getX() / 32;
			int y = e.getY() / 32;
			
			int key = x + y * 4;
			
			if( current != null )
			{
				parent.setSelectedTile( key );
			}
		}

		public void mouseEntered( MouseEvent e ) {}

		public void mouseExited( MouseEvent e ) {}

		public void mousePressed( MouseEvent e ) {}

		public void mouseReleased( MouseEvent e ) {}
	}
}
